package no.ntnu.idi.deid.postprocessor;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

import no.ntnu.idi.deid.config.FilePaths;
import no.ntnu.idi.deid.documentunits.Document;
import no.ntnu.idi.deid.documentunits.Section;
import no.ntnu.idi.deid.documentunits.Sentence;
import no.ntnu.idi.deid.documentunits.Token;
import no.ntnu.idi.deid.preprocessor.Documents;
import no.ntnu.idi.deid.searchresultunits.CategoryTag;

public class DocumentPostProcessor {
	
	
	private DocumentPostProcessor(Documents documentPreProcessor) {
		process(documentPreProcessor);
	}
	
	public static DocumentPostProcessor processDocuments(Documents documentPreProcessor) {
		return new DocumentPostProcessor(documentPreProcessor);
	}
	
	private static int numberOfdocuments = 0;
	private static int numberOfUntaggedTokens = 0;
	private static int totalNumberTokens = 0;
	private static int totalNumberOfSensitiveTokens = 0;
	private static int totalNumberOfNonSensitiveTokens = 0;
	private static int totalNumberOfNonDecidedTokens = 0;
		
	private String generateSearchFieldString(Token token)	{
		totalNumberTokens++;
		if(token.hasSearchResult()) {
			String returnstring = ""; 
			if(!token.hasSearchResult()) {
				numberOfUntaggedTokens++;
				return returnstring + token.getPOSTag();
			}
			else {
				for(CategoryTag t : token) {
					returnstring += t.toString()+",";
					}
				returnstring+= " POS: " + token.getPOSTag();
				return returnstring;
			}
		}
		else {
			return token.getFinalTag()+" " + token.getPOSTag().toString();
		}
	}
	
	private String formatTokenToString(Token token) {
		String sensitivitytag = "";
		if(token.isSensitivityTagDecided()) {
			if(token.isSensitiv()) {
				totalNumberOfSensitiveTokens++;
				sensitivitytag = "Sens";
			}
			else{
				totalNumberOfNonSensitiveTokens++;
				sensitivitytag = "non-Sens";
			}
		}
		else {
			totalNumberOfNonDecidedTokens++;
			sensitivitytag = "N/A";
		}
		return token.getOrginal()+"["+generateSearchFieldString(token)+" SensTag: "+sensitivitytag+" FinalTag: "+token.getFinalTag()+"] ";		
	}
	
	private void process(Documents dpp) {
		for (Document doc : dpp.getDocuments()) {
			numberOfdocuments++;
			regenerateDocument(doc);
		}
		generateStatsDocument(FilePaths.getStatsFilePath());
		DocumentReConstructer.constructDeIdentifiedFiles(dpp);
	}
	
	private void generateStatsDocument(String filename) {
		Writer writer = null;
		try {
			writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(filename), "utf-8"));
			writer.write("Number of documents processed: "+ numberOfdocuments+"\n");
			writer.write("Total number of tokens: "+totalNumberTokens+"\n");
			writer.write("Total number of untagged tokens: "+numberOfUntaggedTokens+"\n");
			writer.write("Total number of sensitive tokens: "+totalNumberOfSensitiveTokens+"\n");
			writer.write("Total number of non-sensitive tokens: "+totalNumberOfNonSensitiveTokens+"\n");
			writer.write("Total number of nondecided: "+totalNumberOfNonDecidedTokens+"\n");
			
			
			double tagged = (((double)totalNumberTokens-(double)numberOfUntaggedTokens)/(double)totalNumberTokens);
			double sens =  (double)totalNumberOfSensitiveTokens / (double)totalNumberTokens;
			double nonsens = (double)totalNumberOfNonSensitiveTokens/(double)totalNumberTokens;
			writer.write("percentage of tagged tokens: "+tagged+"\n");
			writer.write("percentage of sensitive tokens: "+sens+"\n");
			writer.write("percentage of non-sensitive tokens: "+nonsens+"\n");
			
			
			
			} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
				try {
					writer.close();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
		}
	}
		
	private void regenerateDocument(Document doc) {
	String filename = doc.getName();
	Writer writer = null;
	try {
		writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(FilePaths.getOutputFolderPath()+filename), "utf-8"));
		int seksjonsnummer = 1;	
		for (Section sec : doc) {
				writer.write("--------------------------- Seksjonsnummer "+seksjonsnummer+" ------------------------\n");
				seksjonsnummer++;
				for (Sentence sen : sec) {
//					writer.write("\n");
//					String sentence = "";
					for (Token token : sen) {
//						sentence += formatTokenToString(token);	
							writer.write(formatTokenToString(token)+"\n");
//					}
						
					}
//					writer.write(sentence);
				}
			}
		} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} finally{
			try {
				writer.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
	}	
}
}
